using DiLu.Expansion.File.Operates.FTP.Configs;
using FluentFTP;

namespace DiLu.Expansion.File;

/// <summary>
///     FTP扩展
/// </summary>
public static partial class DiLuFileExpansion
{
    /// <summary>
    ///     关闭FTP客户端连接
    /// </summary>
    /// <param name="client">FTP客户端</param>
    public static async ValueTask CloseFtpClientConnectAsync(this AsyncFtpClient client)
    {
        if (client is null) return;
        if (client.IsConnected) await client.Disconnect();
        if (!client.IsDisposed) await client.DisposeAsync();
    }

    /// <summary>
    ///     构建获取FTP文件的配置
    /// </summary>
    /// <param name="rootDirectory">根目录</param>
    /// <param name="fileGroupName">文件分组名称</param>
    /// <param name="fileRelativePath">文件相对路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用<see cref="CloseFtpClientConnectAsync(AsyncFtpClient)" />
    /// </param>
    /// <returns></returns>
    public static GetFtpFileStreamConfig BuildGetFtpFileConfig(this string fileRelativePath,
        string rootDirectory, string fileGroupName, AsyncFtpClient ftpClient, bool autoCloseFtpClient = true)
    {
        return new GetFtpFileStreamConfig(rootDirectory, fileGroupName, fileRelativePath, ftpClient, autoCloseFtpClient);
    }

    /// <summary>
    ///     构建获取FTP文件的配置
    /// </summary>
    /// <param name="fileFullPath">文件完整路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用<see cref="CloseFtpClientConnectAsync(AsyncFtpClient)" />
    /// </param>
    /// <returns></returns>
    public static GetFtpFileStreamConfig BuildGetFtpFileConfig(this string fileFullPath,
        AsyncFtpClient ftpClient, bool autoCloseFtpClient = true)
    {
        return new GetFtpFileStreamConfig(fileFullPath, ftpClient, autoCloseFtpClient);
    }

    /// <summary>
    ///     构建文件流保存到FTP的配置
    /// </summary>
    /// <param name="fileStream">文件流</param>
    /// <param name="rootDirectory">根目录</param>
    /// <param name="fileGroupName">文件分组名称</param>
    /// <param name="fileExtension">文件扩展名</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用<see cref="CloseFtpClientConnectAsync(AsyncFtpClient)" />
    /// </param>
    /// <returns></returns>
    public static SaveFileStreamToFtpConfig BuildSaveFileStreamToFtpConfig(this Stream fileStream,
        string rootDirectory, string fileGroupName, string fileExtension,
        AsyncFtpClient ftpClient, bool autoCloseFtpClient)
    {
        return new SaveFileStreamToFtpConfig(rootDirectory, fileGroupName, fileStream, fileExtension, ftpClient, autoCloseFtpClient);
    }

    /// <summary>
    ///     构建删除FTP文件的配置
    /// </summary>
    /// <param name="rootDirectory">根目录</param>
    /// <param name="fileGroupName">文件分组名称</param>
    /// <param name="fileRelativePath">文件相对路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用<see cref="CloseFtpClientConnectAsync(AsyncFtpClient)" />
    /// </param>
    /// <returns></returns>
    public static DeleteFtpFileConfig BuildDeleteFtpFileConfig(this string fileRelativePath,
        string rootDirectory, string fileGroupName, AsyncFtpClient ftpClient, bool autoCloseFtpClient = true)
    {
        return new DeleteFtpFileConfig(rootDirectory, fileGroupName, fileRelativePath,
            ftpClient, autoCloseFtpClient);
    }

    /// <summary>
    ///     构建删除FTP文件的配置
    /// </summary>
    /// <param name="fileFullPath">文件完整路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用<see cref="CloseFtpClientConnectAsync(AsyncFtpClient)" />
    /// </param>
    /// <returns></returns>
    public static DeleteFtpFileConfig BuildDeleteFtpFileConfig(this string fileFullPath,
        AsyncFtpClient ftpClient, bool autoCloseFtpClient = true)
    {
        return new DeleteFtpFileConfig(fileFullPath, ftpClient, autoCloseFtpClient);
    }

    /// <summary>
    ///     构建删除FTP文件夹的配置
    /// </summary>
    /// <param name="rootDirectory">根目录</param>
    /// <param name="fileGroupName">文件分组名称</param>
    /// <param name="directoryRelativePath">目录相对路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用<see cref="CloseFtpClientConnectAsync(AsyncFtpClient)" />
    /// </param>
    /// <returns></returns>
    public static DeleteFtpFolderConfig BuildDeleteFtpFolderConfig(this string rootDirectory,
        string fileGroupName, string directoryRelativePath, AsyncFtpClient ftpClient, bool autoCloseFtpClient = true)
    {
        return new DeleteFtpFolderConfig(rootDirectory, fileGroupName, directoryRelativePath,
            ftpClient, autoCloseFtpClient);
    }

    /// <summary>
    ///     构建删除FTP文件夹的配置
    /// </summary>
    /// <param name="directoryFullPath">目录完整路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用<see cref="CloseFtpClientConnectAsync(AsyncFtpClient)" />
    /// </param>
    /// <returns></returns>
    public static DeleteFtpFolderConfig BuildDeleteFtpFolderConfig(this string directoryFullPath,
        AsyncFtpClient ftpClient, bool autoCloseFtpClient = true)
    {
        return new DeleteFtpFolderConfig(directoryFullPath, ftpClient, autoCloseFtpClient);
    }

    /// <summary>
    ///     构建搜索FTP文件夹的配置
    /// </summary>
    /// <param name="rootDirectory">根目录</param>
    /// <param name="fileGroupName">文件分组名称</param>
    /// <param name="directoryRelativePath">目录相对路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用 CloseFtpClientConnectAsync()方法
    /// </param>
    /// <param name="searchOption">搜索选项</param>
    /// <param name="searchPattern">
    ///     模糊查询模式，默认匹配所有文件
    ///     如果想查询指定类型文件使用 ^([\s\S]+)(\.(jpg|png))$
    ///     如果想模糊查询文件使用  ^([\s\S]*)name([\s\S]*)(\.\w+)$
    /// </param>
    /// <returns></returns>
    public static SearchFtpFileConfig BuildSearchFtpFileConfig(this string rootDirectory,
        string fileGroupName, string directoryRelativePath, AsyncFtpClient ftpClient, bool autoCloseFtpClient,
        SearchOption searchOption = SearchOption.TopDirectoryOnly,
        string searchPattern = @"^([\s\S]+)(\.\w+)?$")
    {
        return new SearchFtpFileConfig(rootDirectory, fileGroupName, directoryRelativePath,
            ftpClient, autoCloseFtpClient,
            searchOption, searchPattern);
    }

    /// <summary>
    ///     构建搜索FTP文件夹的配置
    /// </summary>
    /// <param name="directoryFullPath">目录完整路径</param>
    /// <param name="ftpClient">FTP客户端</param>
    /// <param name="autoCloseFtpClient">
    ///     自动关闭FTP客户端 默认自动关闭客户端连接
    ///     如果为false需要自行调用 CloseFtpClientConnectAsync()方法
    /// </param>
    /// <param name="searchOption">搜索选项</param>
    /// <param name="searchPattern">
    ///     模糊查询模式，默认匹配所有文件
    ///     如果想查询指定类型文件使用 ^([\s\S]+)(\.(jpg|png))$
    ///     如果想模糊查询文件使用  ^([\s\S]*)name([\s\S]*)(\.\w+)$
    /// </param>
    /// <returns></returns>
    public static SearchFtpFileConfig BuildSearchFtpFileConfig(this string directoryFullPath,
        AsyncFtpClient ftpClient, bool autoCloseFtpClient,
        SearchOption searchOption = SearchOption.TopDirectoryOnly,
        string searchPattern = @"^([\s\S]+)(\.\w+)?$")
    {
        return new SearchFtpFileConfig(directoryFullPath, ftpClient, autoCloseFtpClient, searchOption, searchPattern);
    }
}